<template>
    <div class="role-wrap" style="padding: 6px">
        <el-card>
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
                <el-form-item label="车辆属性" prop="carCode">
                    <el-select v-model="queryParams.carCode" placeholder="车辆属性" clearable style="width: 240px">
                        <el-option label="单位" value="单位" />
                        <el-option label="个人" value="个人" />
                    </el-select>
                </el-form-item>
                <el-form-item label="车牌" prop="carPlate">
                    <el-input v-model="queryParams.carPlate" placeholder="请输入车牌" clearable style="width: 240px"
                        @keyup.enter.native="handleQuery" />
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>

            </el-form>
        </el-card>
        <el-card class="main-card">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
                    <el-button type="success" icon="el-icon-upload" size="mini" @click="openImport">导入</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table class="base-table" v-loading="loading" :data="roleList">
                <el-table-column label="车辆属性" prop="carCode" width="200" align="left" />
                <el-table-column label="车牌号" prop="carPlate" width="200" align="left" />
                <el-table-column label="归属单位/个人名称" prop="cmpName" width="200" align="left" />
                <el-table-column label="车辆状态" align="center" width="100">
                    <template slot-scope="scope">
                        <div v-if="scope.row.status == 0">
                            不可通行
                        </div>
                        <div v-else-if="scope.row.status == 1">
                            可通行
                        </div>
                        <div v-else>
                            识别码作废
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="车辆通行开始时间" align="center" prop="start" width="200">

                </el-table-column>
                <el-table-column label="车辆通行结束时间" prop="end" width="200" align="left" />
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button size="small" type="primary" plain style="padding: 5px" icon="el-icon-edit"
                            @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="small" type="danger" plain style="padding: 5px" icon="el-icon-delete"
                            @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList" />
        </el-card>

        <!-- 添加或修改角色配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
            <div style="margin-top: -55px">
                <el-divider style="margin-top: -30px"></el-divider>
                <el-form ref="form" :model="form" :rules="rules" label-width="160px">
                    <el-form-item label="车辆属性" prop="carCode">
                        <el-select v-model="form.carCode" placeholder="车辆属性" clearable style="width: 240px">
                            <el-option label="单位" value="单位" />
                            <el-option label="个人" value="个人" />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="白名单" prop="whitelistIp">
                        <el-select v-model="form.whitelistIp" multiple placeholder="白名单" clearable style="width: 240px">
                            <el-option v-for="(item, index) in deviceList" :key="index" :label="item.camName"
                                :value="item.camIp" />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="车牌号" prop="carPlate">
                        <el-input v-model="form.carPlate" placeholder="请输入车牌号"></el-input>
                    </el-form-item>
                    <el-form-item label="归属单位/个人名称" prop="cmpName">
                        <el-input v-model="form.cmpName" placeholder="请输入归属单位/个人名称"></el-input>
                    </el-form-item>
                    <el-form-item label="车辆类型" prop="carType">
                        <el-select v-model="form.carType" placeholder="车辆类型" clearable style="width: 240px">
                            <el-option label="卡车" value="卡车" />
                            <el-option label="小轿车" value="小轿车" />
                            <el-option label="面包车" value="面包车" />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="状态" prop="status">
                        <el-select v-model="form.status" placeholder="状态" clearable style="width: 240px">
                            <el-option label="不可通行" value="0" />
                            <el-option label="可通行" value="1" />
                            <el-option label="识别码报废" value="2" />
                        </el-select>
                    </el-form-item>

                    <el-form-item label="车辆通行开始时间" prop="start">
                        <el-date-picker v-model="form.start" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="请输入车辆通行开始时间">
                        </el-date-picker>

                    </el-form-item>
                    <el-form-item label="车辆通行结束时间" prop="end">
                        <el-date-picker v-model="form.end" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="请输入车辆通行结束时间">
                        </el-date-picker>
                    </el-form-item>

                </el-form>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>


        <el-dialog title="导入" :visible.sync="dialogVisible" width="30%">
            <div style="text-align: center;">
                <div>
                    <el-button type="success" icon="el-icon-upload" size="large"
                        @click="handleImport">第一步：下载模板</el-button>
                </div>

                <div>
                    <el-button type="success" icon="el-icon-upload" size="large" @click="handleImport2"
                        style="margin-top: 20px;">第二步：选择文件</el-button>
                </div>
                <div>
                    <el-button type="success" icon="el-icon-upload" size="large" @click="handleImport3"
                        style="margin-top: 20px;">第三步：开始导入</el-button>
                </div>


            </div>
        </el-dialog>


    </div>
</template>

<script>
import { getCarList, getWhiteCarList, addCar, editCar, deleteCar, downloadCarTempate } from "@/api/car/car";
import axios from 'axios';
import { getToken } from '@/utils/auth'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
    dicts: ['sys_normal_disable'],
    components: { Treeselect },
    data() {
        return {
            // 遮罩层
            loading: true,
            dialogVisible: false,

            // 总条数
            total: 0,
            // 角色表格数据
            roleList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,


            // 查询参数
            queryParams: {
                carCode: "",
                carplate: "",
                pageNum: 1,
                pageSize: 10,
                recordType: 1,
            },
            // 表单参数
            form: {},

            // 表单校验
            rules: {
                carCode: [
                    { required: true, message: "车辆属性不能为空", trigger: "change" }
                ],
                whitelistIp: [
                    { required: true, message: "白名单不能为空", trigger: "blur" }
                ],
                carPlate: [
                    { required: true, message: "车牌号不能为空", trigger: "blur" }
                ],
                cmpName: [
                    { required: true, message: "归属单位/个人名称不能为空", trigger: "blur" }
                ],
                carType: [
                    { required: true, message: "车辆类型不能为空", trigger: "change" }
                ],
                status: [
                    { required: true, message: "车辆类型不能为空", trigger: "change" }
                ],
                start: [
                    { required: true, message: "车辆通行开始时间不能为空", trigger: "change" }
                ],
                end: [
                    { required: true, message: "车辆通行结束时间不能为空", trigger: "change" }
                ],
                //start
            },
            deviceList: []
        };
    },
    created() {
        this.getList();
    },
    methods: {
        openImport() {
            this.dialogVisible = true
        },

        //下载模板
        handleImport() {
            let obj = {
                fileName: "车辆信息上传模板-车牌.xlsx"
            }
            // downloadCarTempate(obj).then(res => {
            //     console.log(res);

            // })
            this.download2('system/car_details/template/downLoad', {obj}, `车辆信息上传模板-车牌.xlsx`)
            // axios.get('/dev-api/system/car_details/template/downLoad', {
            //     params: {
            //         fileName: '车辆信息上传模板-车牌.xlsx'
            //     },
            //     headers: {
            //         "Authorization": 'Bearer ' + getToken(),
            //         "Content-type":"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
            //     },

            // }, { responseType: 'blob' }).then(res => {
            //     console.log(res)
            // })


            axios.get('/dev-api/system/car_details/template/downLoad', {
                params: {
                    fileName: "车辆信息上传模板-车牌.xlsx",
                },
                headers: {
                    'Authorization': 'Bearer ' + getToken()
                },
            }, { responseType: 'blob' })
                .then((response) => {
                    console.log(response);

                    const url = window.URL.createObjectURL(new Blob([response.data]));
                    const link = document.createElement('a');
                    link.href = url;
                    link.setAttribute('download', '车辆信息上传模板-车牌.xlsx');
                    document.body.appendChild(link);
                    link.click();
                    document.body.removeChild(link);
                })
                .catch((error) => {
                    console.error(error);
                });
            // this.download2('system/car_details/template/downLoad',obj, `车辆信息上传模板-车牌.xlsx`)
        },
        /** 查询角色列表 */
        getList() {
            this.loading = true;
            getCarList(this.queryParams).then(response => {
                this.roleList = response.data.list;
                this.total = response.data.total;
                this.loading = false;
            }
            );
        },

        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },

        // 表单重置
        reset() {
            this.form = {}
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {

            this.resetForm("queryForm");
            this.handleQuery();
        },

        /** 新增按钮操作 */
        handleAdd() {
            // this.reset();
            this.open = true;
            this.title = "添加";
            this.isEdit = true;
            this.getWhiteCarListFun()

        },

        getWhiteCarListFun() {
            getWhiteCarList().then((res) => {
                if (res.code == 0) {
                    this.deviceList = res.data
                }

            })
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            console.log(row);
            this.getWhiteCarListFun()
            row.status = (row.status).toString()
            row.whitelistIp = row.whitelistIp.split(",")
            this.reset();
            this.form = row;
            this.open = true;
            this.title = "编辑";

        },

        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.form.whitelistIp = (this.form.whitelistIp).toString()
                    if (this.form.id) {

                        editCar(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {

                        addCar(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 提交按钮（数据权限） */
        submitDataScope: function () {
            if (this.form.roleId != 0) {
                this.form.deptIds = this.getDeptAllCheckedKeys();
                dataScope(this.form).then(response => {
                    this.$modal.msgSuccess("修改成功");
                    this.openDataScope = false;
                    this.getList();
                });
            }
        },
        /** 删除按钮操作 */
        handleDelete(row) {

            this.$modal.confirm('是否确认删除?').then(function () {
                return deleteCar(row.id);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/role/export', {
                ...this.queryParams
            }, `role_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
<style>
.role-wrap {
    padding: 6px;
}

.top-card-wrap {
    margin-bottom: 5px;
}

.card-wrap {
    padding-bottom: 100px;
}
</style>